#!/usr/bin/python
# Sample console appliation serving as sensor data receiver and distributor.
# Three devices are supported. An N95 on port 9617, an Android G1 on
# port 9618 and an N97 on port 9619. The data is normalized but sent
# otherwise unchanged to the ports 9642, 9643, and 9644 respectively.

import sys, time
from Sensor import Sensor, UDPSender
import filters


def add_id(block, id):
    block[0]["devid"] = id
    return block

def show_packet(packet):
    print packet

if __name__ == '__main__':
    print " -v verbose"
    sensorn95 = Sensor(9617)
    sensorn95.register_filter(filters.n95normalize)
    sensorn95.register_filter(lambda block: add_id(block, "n95"))
    sensorg1 = Sensor(9618)
    sensorg1.register_filter(filters.androidg1normalize)
    sensorg1.register_filter(lambda block: add_id(block, "g1"))    
    sensorn97 = Sensor(9619)
    sensorn97.register_filter(filters.n97normalize)
    sensorn97.register_filter(lambda block: add_id(block, "n97"))
    if "-v" in sys.argv:
        sensorn95.register_sink(show_packet)
        sensorg1.register_sink(show_packet)
        sensorn97.register_sink(show_packet)
    sendern95 = UDPSender("localhost", 9642)
    sensorn95.register_sink(sendern95)
    senderg1 = UDPSender("localhost", 9643)
    sensorg1.register_sink(senderg1)
    sendern97 = UDPSender("localhost", 9644)
    sensorn97.register_sink(sendern97)
    sensorn95.start_receiving()
    sensorg1.start_receiving()
    sensorn97.start_receiving()
    try:
        time.sleep(2**30) # sleep long!
    except KeyboardInterrupt:
        print "stopping"
    sensorn95.stop_receiving()
    sensorg1.stop_receiving()
    sensorn97.stop_receiving()
    
